package com.gzw.dms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.BackAdatper;
import com.gzw.dms.adapter.QscxAdapter1;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.ClsSetSelected;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;
import com.gzw.dms.view.SwipeRefreshLayout;
import com.jauker.widget.BadgeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderChangeListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener {
    private Toolbar toolbar;
    private Context mContext;
    private QscxAdapter1 mAdapter;
    private List<ShippingOrderInfo> lsMapInfo = new ArrayList<>();
    private List<ShippingOrderInfo> shippingOrderInfo;
    private Intent mIntent;
    private View v1;
    private String  state = "";
    private  String fpstste="1";
    private SwipeRefreshLayout mSwipeLayout;
    private RecyclerView mRecyclerView;
    enum RefreshOrLoad {
        REFRESH, LOAD
    }
    private RefreshOrLoad refreshOrLoad = RefreshOrLoad.REFRESH;
    private String page = "10";
    private String row = "0";
    private String selectId = "";
    private String send_time ="";
    private String end_time ="";
    private String shiping_order_num = "";
    private String shiping_order_goid = "";
    private String send_mechanism = "";
    private String custom_name = "";
    private String receipt_name = "";
    private String end_address = "";
    private String  receipt_tel = "";
    private String end_mechanism = "";
    private  String type = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_order_change_list);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mRecyclerView = (RecyclerView) findViewById(R.id.agreement_listView);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("运单调整");
        mIntent = getIntent();
        setSupportActionBar(toolbar);
//        selectId = (String)mIntent.getExtras().get("driver_id");

        mRecyclerView.setHasFixedSize(true);
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加默认分割线
//        mRecyclerView.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL));
        //添加自定义分割线
        //mRecyclerView.addItemDecoration(new RecycleViewDivider(
        //mContext, LinearLayoutManager.HORIZONTAL, R.drawable.divider_bg));
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setOnLoadListener(this);
        mSwipeLayout.setColor(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeLayout.setLoadNoFull(false);
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(page, row, selectId, send_time,end_time,shiping_order_num,shiping_order_goid,send_mechanism,end_address,custom_name,receipt_name,receipt_tel,end_mechanism,type);
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new myOnClick());

    }


    @Override
    public void onRefresh() {
        page = "10";
        refreshOrLoad = RefreshOrLoad.REFRESH;
        row = "0";
        send_time = "";
        end_time = "";
        shiping_order_num = "";
        send_mechanism = "";
        receipt_name = "";
        end_address = "";
        receipt_tel = "";
        end_mechanism = "";
        custom_name = "";
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(page, row, selectId, send_time,end_time,shiping_order_num,shiping_order_goid,send_mechanism,end_address,custom_name,receipt_name,receipt_tel,end_mechanism,type);
    }
    @Override
    public void onLoad() {
        row = String.valueOf(mAdapter.getItemCount() + 1);
        refreshOrLoad = RefreshOrLoad.LOAD;
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(page, row, selectId, send_time,end_time,shiping_order_num,shiping_order_goid,send_mechanism,end_address,custom_name,receipt_name,receipt_tel,end_mechanism,type);
    }
    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                default:
                    finish();
                    break;
            }
        }
    }
    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

            if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                mSwipeLayout.setRefreshing(true);

            } else {
                mSwipeLayout.setLoading(true);
            }
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("page", params[0]);
            map.put("row", params[1]);
            map.put("selectId", params[2]);
            map.put("send_time", params[3]);
            map.put("end_time", params[4]);
            map.put("shiping_order_num", params[5]);
            map.put("shiping_order_goid",params[6]);
            map.put("custom_name",params[7]);
            map.put("end_address",params[8]);
            map.put("send_mechanism",params[9]);
            map.put("receipt_name",params[10]);
            map.put("receipt_tel",params[11]);
            map.put("end_mechanism",params[12]);
            map.put("type", params[13]);

            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appSearchShipperOrderInfoGroupByRole"), "json=" + json);
//            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appSearchShipperOrderInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                mSwipeLayout.setRefreshing(false);
            } else {
                mSwipeLayout.setLoading(false);
            }
            if (result == null || result.length() == 0) {
                T.showShort(mContext, "连接服务器异常。");
            }
            if (result.indexOf("successMsg") > 0) {
                String successMsg = JsonUtil.getMsg(result, "successMsg");
                String list = JsonUtil.getMsg(successMsg, "list");


                final List<ShippingOrderInfo> lsMap = JsonUtil.getPersons(list, new TypeToken<List<ShippingOrderInfo>>() {
                }.getType());
                if (refreshOrLoad == RefreshOrLoad.REFRESH) {
                    lsMapInfo.clear();
                    lsMapInfo.addAll(lsMap);
                } else {
                    lsMapInfo.addAll(lsMap);

                }
                if (mAdapter == null) {
                    mRecyclerView.setAdapter(mAdapter = new QscxAdapter1(lsMapInfo));
                } else {
                    mAdapter.notifyDataSetChanged();
                }
                mAdapter.setOnRecyclerViewListener(new QscxAdapter1.OnRecyclerViewListener() {
                    @Override
                    public void onItemClick(int position) {

                        ShippingOrderInfo shippingOrder =  lsMapInfo.get(position);
                        Intent mIntetn = new Intent();
                        Bundle bundle = new Bundle();
                        mIntetn.setClass(mContext, OrderChangeDetailActivity.class);
                        bundle.putSerializable("shippingOrder", shippingOrder);
                        bundle.putString("state", state);
                        mIntetn.putExtra("fpstste","4");
                        mIntetn.putExtras(bundle);
                        startActivityForResult(mIntetn, 1);
                    }

                    @Override
                    public boolean onItemLongClick(int position) {
                        return false;
                    }
                });
            } else if (result.indexOf("errorMsg") > 0) {
                T.showShort(mContext, JsonUtil.getMsg(result, "errorMsg"));
            }
        }
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {
            refreshOrLoad = RefreshOrLoad.REFRESH;

            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute(page, row, selectId, send_time,end_time,shiping_order_num,shiping_order_goid,send_mechanism,end_address,custom_name,receipt_name,receipt_tel,end_mechanism,type);
        }

        else if (resultCode == 2) {
            refreshOrLoad = RefreshOrLoad.REFRESH;
            shiping_order_num = (String) data.getExtras().get("order_num");
            send_time = (String) data.getExtras().get("startTime");
            end_time = (String) data.getExtras().get("endTime");
            shiping_order_goid = (String) data.getExtras().get("shiping_order_goid");
            custom_name
                    = (String) data.getExtras().get("custom_name");
            receipt_name
                    = (String) data.getExtras().get("receipt_name");

            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute(page, row, selectId, send_time,end_time,shiping_order_num,shiping_order_goid,send_mechanism,end_address,custom_name,receipt_name,receipt_tel,end_mechanism,type);
        }

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu_home; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_vehicle, menu);
        getMenuInflater().inflate(R.menu.menu_select, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_select:
                Intent myIntetn = new Intent();
                myIntetn.putExtra("back","1");
                myIntetn.setClass(mContext, SelectActivity.class);
                startActivityForResult(myIntetn, 1);

                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
